5 Lecture
CS201
Midterm & Final Term Short Notes
Conditional Statements
Conditional statements are programming constructs that allow a program to make decisions based on certain conditions. They use if-then logic to determine whether to execute certain sections of code. Conditional statements typically involve compa
Important Mcq's
Midterm & Finalterm Prepration
Past papers included
Download PDF
- What is the syntax for an if statement in Python?
A. if (condition): B. if [condition]: C. if {condition}: D. if <condition>:
Answer: A
- What is the output of the following code snippet?
x = 5 if x > 3: print("x is greater than 3") else: print("x is less than or equal to 3")
A. x is greater than 3 B. x is less than or equal to 3 C. SyntaxError D. NameError
Answer: A
- Which conditional statement can handle multiple conditions in Python?
A. if statement B. if-else statement C. if-elif statement D. switch statement
Answer: C
- What is the syntax for an if-else statement in JavaScript?
A. if (condition) {code block 1;} else {code block 2;} B. if [condition] {code block 1;} else {code block 2;} C. if {condition} {code block 1;} else {code block 2;} D. if <condition> {code block 1;} else {code block 2;}
Answer: A
- What is the output of the following code snippet?
x = 10 if x > 5: print("x is greater than 5") elif x > 8: print("x is greater than 8") else: print("x is less than or equal to 5")
A. x is greater than 5 B. x is greater than 8 C. x is less than or equal to 5 D. SyntaxError
Answer: A
- Which conditional statement is used to execute different code blocks for different values of a variable?
A. if statement B. if-else statement C. if-elif statement D. switch statement
Answer: D
- What is the output of the following code snippet?
x = 10 if x > 5 and x < 15: print("x is between 5 and 15")
A. x is between 5 and 10 B. x is between 10 and 15 C. x is greater than 5 and less than 15 D. x is not between 5 and 15
Answer: C
- Which of the following is an example of a Boolean expression?
A. 5 + 3 B. "Hello, world!" C. True D. None
Answer: C
- What is the output of the following code snippet?
x = "apple" if "p" in x: print("The letter p is in x")
A. The letter p is in x B. The letter p is not in x C. SyntaxError D. NameError
Answer: A
- What is the output of the following code snippet?
x = 5 if x == 5: x = x + 1 if x > 5: x = x + 2 print(x)
A. 5 B. 6 C. 7 D. 8
Answer: C
Subjective Short Notes
Midterm & Finalterm Prepration
Past papers included
Download PDF
What is a conditional statement? Answer: A conditional statement is a programming construct that allows a program to make decisions based on certain conditions. It uses if-then logic to determine whether to execute certain sections of code.
What is the difference between if and if-else statements? Answer: An if statement executes a section of code if a condition is true. An if-else statement executes one section of code if the condition is true, and another section of code if the condition is false.
What is the syntax for an if statement in Java? Answer: The syntax for an if statement in Java is: if (condition) { // code to execute if condition is true }
What is a Boolean expression? Answer: A Boolean expression is an expression that evaluates to either true or false.
What is the purpose of the else keyword in an if-else statement? Answer: The else keyword in an if-else statement is used to execute a section of code if the condition in the if statement is false.
What is the syntax for an if-elif statement in Python? Answer: The syntax for an if-elif statement in Python is: if condition1:
code to execute if condition1 is true
elif condition2: # code to execute if condition2 is true else: # code to execute if neither condition1 nor condition2 is true
What is the difference between the == and = operators in Python? Answer: The == operator is used to compare two values for equality, while the = operator is used to assign a value to a variable.
What is short-circuit evaluation in conditional statements? Answer: Short-circuit evaluation is a process in which a conditional statement stops evaluating expressions as soon as it can determine the outcome based on earlier evaluations.
What is the syntax for a switch statement in C++? Answer: The syntax for a switch statement in C++ is: switch (expression) { case value1: // code to execute if expression == value1 break; case value2: // code to execute if expression == value2 break; default: // code to execute if expression does not match any of the cases }
What is the purpose of the break keyword in a switch statement? Answer: The break keyword in a switch statement is used to exit the switch statement after executing the code in the matching case. Without a break statement, the code in the next case will also be executed.